package com.dmall.ssm.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dmall.ssm.hystrix.UserCommand;
import com.dmall.ssm.param.QueryCriterion;
import com.dmall.ssm.request.UserRequestPara;
import com.dmall.ssm.result.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dmall.ssm.pojo.User;
import com.dmall.ssm.service.UserService;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;

@Controller
@Slf4j
public class UserController {

	@Autowired
	private UserService userService;
	
	@RequestMapping("/user/{id}")
	public String get(@PathVariable("id") Integer id, Model model) {
		User user = userService.findById(id);
		model.addAttribute("user", user);
		log.info("【根据用户ID查询用户信息】, user={}", user);
		return null;
	}
	
	@RequestMapping("/user/contains/{id}/{contains}")
	@ResponseBody
	public List<User> containsId(@PathVariable("id") Integer id, @PathVariable("contains") Integer contains) {
		return userService.findByContains(id, contains);
	}
	
	
	@RequestMapping("/user/list")
	public String list(Model model) {
		List<User> list = userService.findAll();
		model.addAttribute("list", list);
		log.info("【查询所有用户信息】, list={}", list);
		return "user/list";
	}
	
	@RequestMapping("/rest/user/list")
	@ResponseBody
	public List<User> list() {
		List<User> list = userService.findAll();
		return list;
	}

	@RequestMapping("/user/not/{id}")
	@ResponseBody
	public List<User> notContains(@PathVariable("id") Integer id) {
		List<User> list = userService.notContains(id);
		return list;
	}

	@RequestMapping("/user/findByCriterion")
	@ResponseBody
	public List<User> findByCriterion(@RequestParam("id") Integer id) {
		QueryCriterion queryCriterion = new QueryCriterion();
		queryCriterion.eq("id", id);
		queryCriterion.in("gender", Arrays.asList(1));
		List<User> users = userService.findByCriterion(queryCriterion);
		return users;
	}

	@ResponseBody
	@RequestMapping("/user/test/hystrix")
	public CommonResult testHystrix() throws Exception {
		UserRequestPara para = new UserRequestPara();
		para.setUserId(1);
		para.setUserName("zhangsan");
		CommonResult result = new UserCommand(userService, para).execute();
		return result;
	}
}
